//
//  ZhiFuViewController.m
//  大股票
//
//  Created by apple on 2019/9/5.
//  Copyright © 2019 apple. All rights reserved.
//

#import "ZhiFuViewController.h"

@interface ZhiFuViewController ()
@property(strong,nonatomic)NSMutableArray* dataArr;
@property(strong,nonatomic)NSMutableDictionary* dataDic;
@end

@implementation ZhiFuViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"支付密码";
    self.dataArr=[[NSMutableArray alloc] init];
    [self.dataArr addObjectsFromArray:kUserDefaultObjectForKey(@"user")];
    self.dataDic=[[NSMutableDictionary alloc] init];
    self.dataDic=[NSMutableDictionary dictionaryWithDictionary:self.dataArr[self.number]];
    if ([self.dataArr[self.number][@"zhifupwd"] isEqualToString:@"无"]) {
        self.jiuline.hidden=YES;
        self.jiuLabel.hidden=YES;
        self.jiuTextfield.hidden=YES;
        [self.pwdBtn setTitle:@"确认设置" forState:UIControlStateNormal];
    }
}

- (IBAction)sureChange:(id)sender {
    if ([self.dataArr[self.number][@"zhifupwd"] isEqualToString:@"无"]){

       if (self.zhifuTextfield.text.length==0){
            [self.view makeToast:@"请输入新密码"
                        duration:2.0
                        position:CSToastPositionCenter];
       }else if (self.againPwd.text.length!=6){
           [self.view makeToast:@"支付密码6位~"
                       duration:2.0
                       position:CSToastPositionCenter];
       }else if (![self.againPwd.text isEqualToString:self.zhifuTextfield.text]){
            [self.view makeToast:@"两次密码输入不一致~"
                        duration:2.0
                        position:CSToastPositionCenter];
        }else{
                [self.dataDic setObject:self.zhifuTextfield.text forKey:@"zhifupwd"];
                [self.dataArr removeObjectAtIndex:self.number];
                [self.dataArr insertObject:self.dataDic atIndex:self.number];
                kUserDefaultSetObjectForKey(self.dataArr, @"user");
                MBProgressHUD* HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
                HUD.completionBlock = ^(){
                    [self.view makeToast:@"设置成功~" duration:2.0 position:CSToastPositionCenter title:nil image:nil style:nil completion:^(BOOL didTap) {
                        [self.navigationController popViewControllerAnimated:YES];
                    }];
                };
                [HUD hideAnimated:YES afterDelay:1.0];
        }
    }else{
        self.jiuline.hidden=NO;
        self.jiuLabel.hidden=NO;
        self.jiuTextfield.hidden=NO;
        if (self.jiuTextfield.text.length==0) {
            [self.view makeToast:@"请输入旧密码"
                        duration:2.0
                        position:CSToastPositionCenter];
        }else if (self.zhifuTextfield.text.length==0){
            [self.view makeToast:@"请输入新密码"
                        duration:2.0
                        position:CSToastPositionCenter];
        }else if (self.againPwd.text.length==0){
            [self.view makeToast:@"请确认新密码"
                        duration:2.0
                        position:CSToastPositionCenter];
        }else if (self.againPwd.text.length!=6){
            [self.view makeToast:@"支付密码6位~"
                        duration:2.0
                        position:CSToastPositionCenter];
        }
        else if (![self.againPwd.text isEqualToString:self.zhifuTextfield.text]){
            [self.view makeToast:@"两次密码输入不一致~"
                        duration:2.0
                        position:CSToastPositionCenter];
        }else{
            if (![self.jiuTextfield.text isEqualToString:self.dataDic[@"zhifupwd"]]) {
                [self.view makeToast:@"旧密码错误"
                            duration:2.0
                            position:CSToastPositionCenter];
            }else{
                [self.dataDic setObject:self.zhifuTextfield.text forKey:@"zhifupwd"];
                [self.dataArr removeObjectAtIndex:self.number];
                [self.dataArr insertObject:self.dataDic atIndex:self.number];
                kUserDefaultSetObjectForKey(self.dataArr, @"user");
                MBProgressHUD* HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
                HUD.completionBlock = ^(){
                    [self.view makeToast:@"修改成功~" duration:2.0 position:CSToastPositionCenter title:nil image:nil style:nil completion:^(BOOL didTap) {
                        [self.navigationController popViewControllerAnimated:YES];
                    }];
                };
                [HUD hideAnimated:YES afterDelay:1.0];
            }
        }
    }
}

@end
